import {reactive} from "vue";
import type {FormRules} from "element-plus";
import {isPhone, isEmail} from "@pureadmin/utils";

/** 自定义表单规则校验 */
export const formRules = reactive(<FormRules>{
  nickname: [{required: true, message: "用户昵称为必填项", trigger: "blur"}],
  username: [{required: true, message: "用户名称为必填项", trigger: "blur"}],
  password: [{required: true, message: "用户密码为必填项", trigger: "blur"}],
  mobile: [
    {
      validator: (rule, value, callback) => {
        if (value === "") {
          callback();
        } else if (!isPhone(value)) {
          callback(new Error("请输入正确的手机号码格式"));
        } else {
          callback();
        }
      },
      trigger: "blur"
      // trigger: "click" // 如果想在点击确定按钮时触发这个校验，trigger 设置成 click 即可
    }
  ],
  email: [
    {
      validator: (rule, value, callback) => {
        if (value === "") {
          callback();
        } else if (!isEmail(value)) {
          callback(new Error("请输入正确的邮箱格式"));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ],
  deptIds: [
    {
      required: true,
      message: "归属部门为必选项",
      trigger: "change",
      type: "array"
    }
  ],
  primaryPositionId: [
    {
      required: true,
      message: "主岗位为必选项",
      trigger: "change"
    }
  ]
});
